<?php

namespace Crawler\Request;

/**
 * PHP version 5.3+
 * CrawlerRequest handles each
 * http Request uses normal file_get_contents method.
 * Note:- Inherits CrawlerRequestAbstract
 *
 * @author    Ravi Shanker B
 * @package   WebCrawler
 */
use Crawler\Request\RequestAbstract;

class RequestRegular extends RequestAbstract{

    /*
     * Sets web crawler with
     *
     * @param string $uri   URL
     * @return array $links all links as an array
     */
    public function getLinks($url){

        //for ssl
        $ctx = stream_context_create(
            array(
              'http'=>array(
                'header'=>"Content-type: application/x-www-form-urlencoded",
              )
            )
          );

        $HTML = file_get_contents($url,0,$ctx);
        $doc = new \DOMDocument();
        @$doc->loadHTML($HTML);
        $links = array();
        foreach ($doc->getElementsByTagName('a') as $element) {
            if ($element->hasAttribute('href')) {
                $links[] = $element->getAttribute('href');
            }
        }
        return $links;
    }
    

    public function getTable($url){

        $arraytiso = array();
        //for ssl
        $ctx = stream_context_create(
            array(
              'http'=>array(
                'header'=>"Content-type: application/x-www-form-urlencoded",
              )
            )
          );

        $HTML = file_get_contents($url,0,$ctx);
        $doc = new \DOMDocument();
        @$doc->loadHTML($HTML);
        $doc->preserveWhiteSpace = false;
        $data = $doc->getElementById('zone-season-matches-by-round-run');
        $doc->saveHTML($data);
        //CONTENT OF DATA
        $data->C14N();

        $tr = array();
        // $tables = $doc->getElementsByTagName('table');
        // $this->get_inner_html($tables->item(0));
        // $i = 0;
        $trtext = '';
        // while($table = $tables->item($i++))
        // {
        //     $tr[] = $doc->saveHTML($table);
        //     // foreach($table->attributes as $attr)
        //     // {
        //     //     $trtext .= $attr->name . " " . $attr->value . "<br />";
        //     // }
        //     $class_node = $table->attributes->getNamedItem('class');
     
        //     if($class_node)
        //     {
        //         $trtext .= "Class is : " . $table->attributes->getNamedItem('class')->value . PHP_EOL . "</br>";
        //     }
        // }

        $tables = $doc->getElementsByTagName('table');
 
        $table = $tables->item(0);
        echo '<pre>';
        //print_r($doc->saveHTML($table));
        foreach($table->childNodes as $key1=>$child)
        {   
            //echo $k;
            $arrtmp=array();
            if($key1 > 0){
              foreach ($child->childNodes as $key2 => $value2) {
                # code...
                //echo $key2;
                if($key2%2 == 0 && $key2 < 10){
                  //print_r($value->nodeValue);
                  //$arraytiso[];
                  
                  foreach ($value2->childNodes as $key3 => $value3) {
                    
                    if($value3->nodeName != '#text'){
                      //echo $key3;
                      //print_r($value3->childNodes->length);
                      if($value3->childNodes->length == 2){

                        $arrtmp[]=$value3->firstChild->nodeValue;

                        $arrtmp[]=$value3->lastChild->nodeValue;

                      }else{
                        $arrtmp[]=$value3->textContent;
                      }
                      //print_r($value3->ownerDocument->saveHTML($value3));
                      //   

                      
                    }
                    
                  }
                  

                }
              }
              $param = array();
              //Thoi gian dien ra
              $param['live_at']=$arrtmp[0];
              //Doi tuyen
              $param['team1']=$arrtmp[1];
              $param['team2']=$arrtmp[2];
              //Ti so keo chau a
              $param['rate_asia'][0]=$arrtmp[3];
              $param['rate_asia'][1]=$arrtmp[4];
              $param['rate_asia'][2]=$arrtmp[5];
              //Ti so keo tai xiu
              $param['rate_bet'][0]=$arrtmp[6];
              $param['rate_bet'][1]=$arrtmp[7];
              $param['rate_bet'][2]=$arrtmp[8];
              $param['rate_bet'][3]=$arrtmp[9];
              //Ti so keo chau au
              $param['rate_euro'][0]=$arrtmp[10];
              $param['rate_euro'][1]=$arrtmp[11];
              $param['rate_euro'][2]=$arrtmp[12];

              $arraytiso[] = $param;

              
              //die();
            }
            //print_r($child->ownerDocument->saveHTML($child));
             
        }

        return $arraytiso;
        //$table2 = $doc->getElementById('data');
 
      //var_dump($table->isSameNode($table2)); 
        //get the number of rows in the 2nd table
        //echo $table->childNodes->length; 
         
        //content of each child
        foreach($table->childNodes as $child)
        {
            $trtext .= $child->ownerDocument->saveHTML($child);
        }

        //return $trtext;
        //return $data;
    }


    public function getRate($url){

        $arraytiso = array();
        //for ssl
        $ctx = stream_context_create(
            array(
              'http'=>array(
                'header'=>"Content-type: application/x-www-form-urlencoded",
              )
            )
          );

        $HTML = file_get_contents($url,0,$ctx);
        $doc = new \DOMDocument();
        @$doc->loadHTML($HTML);
        $doc->preserveWhiteSpace = false;
        $data = $doc->getElementById('zone-season-matches-by-round-run');
        $doc->saveHTML($data);
        
        $tables = $doc->getElementsByTagName('table');
 
        $table = $tables->item(0);

        foreach($table->childNodes as $key1=>$child)
        {   
            $arrtmp=array();
            if($key1 > 0){
              foreach ($child->childNodes as $key2 => $value2) {
                # code...
                if($key2%2 == 0 && $key2 < 10){
                  
                  foreach ($value2->childNodes as $key3 => $value3) {
                    
                    if($value3->nodeName != '#text'){
                      
                      if($value3->childNodes->length == 2){

                        $arrtmp[]=$value3->firstChild->nodeValue;

                        $arrtmp[]=$value3->lastChild->nodeValue;

                      }else{
                        $arrtmp[]=$value3->textContent;
                      }
                      
                    }
                    
                  }
                  

                }
              }
              $param = array();
              //Thoi gian dien ra
              $param['live_at']=$arrtmp[0];
              //Doi tuyen
              $param['team1']=$arrtmp[1];
              $param['team2']=$arrtmp[2];
              //Ti so keo chau a
              $param['rate_asia'][0]=$arrtmp[3];
              $param['rate_asia'][1]=$arrtmp[4];
              $param['rate_asia'][2]=$arrtmp[5];
              //Ti so keo tai xiu
              $param['rate_bet'][0]=$arrtmp[6];
              $param['rate_bet'][1]=$arrtmp[7];
              $param['rate_bet'][2]=$arrtmp[8];
              $param['rate_bet'][3]=$arrtmp[9];
              //Ti so keo chau au
              $param['rate_euro'][0]=$arrtmp[10];
              $param['rate_euro'][1]=$arrtmp[11];
              $param['rate_euro'][2]=$arrtmp[12];

              $arraytiso[] = $param;

            }
            
        }

        return $arraytiso;
    }
    
     
    function get_inner_html( $node ) 
    {
        $innerHTML= '';
        $children = $node->childNodes;
         
        foreach ($children as $child)
        {
            $innerHTML .= $child->ownerDocument->saveXML( $child );
        }
         
        return $innerHTML;
    }
}